import { Bean, Configuration } from "boot/decorator"
import { DataSource, TransactionManager, CurdTemplate } from "boot/db"

@Configuration()
export class AppConfig {

  @Bean()
  dataSource(): DataSource {
    const mysqlConfig = {
      // 服务器地址
      host: '127.0.0.1',
      // 端口
      port: 3306,
      // 数据库编码默认采用utf8/utf8mb4
      charset: 'utf8mb4',
      // 数据库名
      database: 'book',
      // 用户名
      user: 'root',
      // 密码
      password: '123456',
      connectTimeout: 10000,
      supportBigNumbers: true,
      bigNumberStrings: true,
      // 同时使用多条查询语句
      multipleStatements: true,
      // https://stackoverflow.com/questions/71949351/how-to-use-named-parameters-in-node-mysql2-prepared-statements
      namedPlaceholders: true,
      waitForConnections: true,
      connectionLimit: 16,
      queueLimit: 0
    }
    return new DataSource(mysqlConfig)
  }

  @Bean()
  public transactionManager(dataSource: DataSource): TransactionManager {
    return new TransactionManager(dataSource)
  }

  @Bean()
  public curdTemplate(transactionManager: TransactionManager): CurdTemplate {
    return new CurdTemplate(transactionManager)
  }

}
